package org.september.pisces.sms.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.september.pisces.settings.service.PiscesSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.hutool.http.HttpUtil;

/**
 * 飞鸽短信发送
 * https://e.4321.sh
 * 账号:187 6844 5463
 * 密码:dd123456(子账号密码也是这个)
 * @author xzye
 *
 */
@Service
public class FeiGeSmsService implements GeneralSmsService{
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String url = "https://api.4321.sh/sms/template";
	
//	private static final String apikey = "N90694942c";
//	
//	private static final String secret = "90694e571dc5ca83";
//	
//	//东鼎治超综合管理平台 短信签名id
//	private static final String sign_id = "170021";
	
	@Autowired
	private PiscesSettingService settingService;
	
	public static void main(String[] args) {
//		FeiGeSmsService ss = new FeiGeSmsService();
//		ss.sendOverWeightSms("13395594089,18768445463", "浙A77AA9","南洋河桥","2023-02-06 12:32:23","1.82吨","2轴","20%");
	}
	
//	public void sendOverWeightSms(WeightData data, String tels) {
//		SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String[] arr = new String[6] ;
//		arr[0]=data.getPlateNumber();
//		arr[1]=data.getStation();
//		arr[2]=yyyy_MM_dd_HH_mm_ss.format(data.getPassTime());
//		arr[3]=String.valueOf(data.getWeight().floatValue());
//		arr[4]=String.valueOf(data.getAxelCount());
//		if(data.getOverWeightRate()==null) {
//			arr[5]="0%";
//		}else {
//			DecimalFormat df = new DecimalFormat("0.00");
//			arr[5] =df.format(data.getOverWeightRate().floatValue()*100)+"%";
//		}
//		send(tels.split(","),String.join("||", arr));//变量
//	}
	
	public void send(String tels, String content) {
		Map<String,Object> params = new HashMap<>();
		params.put("apikey", settingService.getValueOrDefault("飞鸽短信设置","sms_app_key","","飞鸽apikey",""));
		params.put("secret", settingService.getValueOrDefault("飞鸽短信设置","sms_app_secret","","飞鸽授权码",""));
		params.put("sign_id", settingService.getValueOrDefault("飞鸽短信设置","sms_sign_id","","飞鸽签名id",""));
		//发现超载车辆，车牌号码为$$，位置：$$，过车时间：$$，重量：$$，轴数：$$，超载率:$$
		params.put("template_id", settingService.getValueOrDefault("飞鸽短信设置","sms_tmpl_id","","飞鸽短信模板id",""));
		params.put("mobile", tels);
		params.put("content", content);//变量
		logger.info("发送短信告警："+JSON.toJSONString(params));
		String result = HttpUtil.post(url, JSON.toJSONString(params));
		logger.info("收到回复："+result);
	}

	@Override
	public boolean send(String[] tels, String content) {
		try {
			send(String.join(",", tels) , content);
			return true;
		}catch(Exception ex) {
			return false;
		}
	}
}
